package com.cyh.controller;

import com.cyh.domain.User;
import com.cyh.domain.VO;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.UrlBasedViewResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    @RequestMapping("/quick") //最终路径是: /user/quick
    //@RequestMapping(value = "/quick", method = RequestMethod.GET, params = {"username"})
    public String save() {
        System.out.println("Controller save running ...");
        //return "success.jsp"; //这个指的是在user文件夹下找资源
        //return "/success.jsp";  //指的是根目录web文件夹下找资源 相当于 forward:/success.jsp
        /*UrlBasedViewResolver.REDIRECT_URL_PREFIX 是“forward:” 转发； url不会改变
        UrlBasedViewResolver.REDIRECT_URL_PREFIX 是“redirect:” 重定向； url会改变,因为有些资源无法在外部访问，所以要重定向*/

        return "test1";
    }

    @RequestMapping("/quick2")
    public ModelAndView save2() {
        /*Model: 模型，作用是封装数据；
        View: 视图，作用展示数据*/
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("username", "陈--");
        //modelAndView.setViewName("/success.jsp");
        modelAndView.setViewName("test1");
        return modelAndView;
    }

    @RequestMapping("/quick3")
    public ModelAndView save3(ModelAndView modelAndView) {

        modelAndView.addObject("username", "陈二二");
        modelAndView.setViewName("test1");
        return modelAndView;
    }

    @RequestMapping("/quick4")
    public String save4(Model model) {

        model.addAttribute("username", "陈三三");
        return "test1";
    }

    @RequestMapping("/quick5")
    public String save5(HttpServletRequest request) {
        //不常用，最好还是用上面的框架
        request.setAttribute("username", "陈四");
        return "test1";
    }

    @RequestMapping("/quick6")
    public void save6(HttpServletResponse response) throws IOException {
        //不推荐使用，最好使用框架
        response.getWriter().println("回写数据，不返回页面，直接回在当前url下，显示这些文字");//会中文乱码，后续解决
    }

    @RequestMapping("/quick7")
    @ResponseBody //加了这个注解，表明返回的是回写数据，而不是页面跳转
    public String save7() {
        return "hello, chen 777";
    }

    @RequestMapping("/quick8")
    @ResponseBody
    public String save8() {
        return "{\"username\":\"chen 8888\", \"age\": \"18\"}";
    }

    @RequestMapping("/quick9")
    @ResponseBody
    public String save9() throws JsonProcessingException {
        User user = new User();
        user.setUsername("chen 9999");
        user.setAge(19);
        //pom中要增加三个json库
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(user);
        return json;
    }

    @RequestMapping("/quick10")
    @ResponseBody
    public User save10(){
        User user = new User();
        user.setUsername("chen 101010");
        user.setAge(20);
        return user;
    }

    @RequestMapping("/quick11")
    @ResponseBody
    public void save11(String username, int age){
        //返回void,不会跳转页面，也不会显示数据
        //http://localhost:8080/003MySpringMvc_war/user/quick11?username=chen&age=21
        System.out.println(username);
        System.out.println(age);

    }

    @RequestMapping("/quick12")
    @ResponseBody
    public void save12(User user){
        //http://localhost:8080/003MySpringMvc_war/user/quick12?username=chen2&age=212
        System.out.println(user);
    }

    @RequestMapping("/quick13")
    @ResponseBody
    public void save13(String[] strs){
        //http://localhost:8080/003MySpringMvc_war/user/quick13?strs=aaa&strs=bbb&strs=ccc
        System.out.println(Arrays.asList(strs));
    }

    @RequestMapping("/quick14")
    @ResponseBody
    public void save14(VO userList){
        //http://localhost:8080/003MySpringMvc_war/form.jsp
        //传集合类型，在form.jsp使用post发送集合,但是中文乱码，后续改web.xml
        System.out.println(userList);
    }

    @RequestMapping("/quick15")
    @ResponseBody
    public void save15(@RequestBody List<User> userList){
        //传集合类型，使用ajax.jsp传，使用post请求，传json格式数据，不需要包装，直接用List<User>接收，但是需要加@RequestBody
        //http://localhost:8080/003MySpringMvc_war/ajax.jsp
        //要在spring-mvc.xml中加<mvc:resources mapping="/js/**" location="/js/"/>
        //ajax.jsp 中<script></script>开头和结尾要匹配
        System.out.println(userList);
    }

    @RequestMapping("/quick16")
    @ResponseBody
    public void save16(@RequestParam(value = "name", required = false, defaultValue = "aaa") String username){
        //http://localhost:8080/003MySpringMvc_war/user/quick16?name=ccc
        //@RequestParam 可以将请求地址的其它参数名转成指定参数名
        //有三个参数：value, required:默认true,表示必须填写，defaultValue:默认值
        System.out.println(username);
    }

    @RequestMapping("/quick17/{name}")
    @ResponseBody
    public void save17(@PathVariable("name") String name){
        //http://localhost:8080/003MySpringMvc_war/user/quick17/bbb
        System.out.println(name);
    }

    @RequestMapping("/quick18")
    @ResponseBody
    public void save18(Date date){
        //http://localhost:8080/003MySpringMvc_war/user/quick18?date=2021/9/29
        //打印：Wed Sep 29 00:00:00 CST 2021， 能打印出日期，但是换成2021-9-29就会报错，所以需要加转换器，但是加了转换器就不能使用默认的2021/9/29否则报错
        System.out.println(date);
    }

    //日期转换器
    @RequestMapping("/quick19")
    @ResponseBody
    public void save19(Date date){
        //1,定义转换器类 DateConverter
        //2,在spring-mvc.xm文件中声明转换器
        //3,在spring-mvc.xml中的注解驱动中添加 conversion-service
        //http://localhost:8080/003MySpringMvc_war/user/quick18?date=2021-9-29
        //打印：Wed Sep 29 00:00:00 CST 2021
        System.out.println(date);
    }

    @RequestMapping("/quick20")
    @ResponseBody
    public void save20(HttpServletRequest request, HttpServletResponse response, HttpSession session){
        System.out.println(request);
        System.out.println(response);
        System.out.println(session);
    }

    @RequestMapping("/quick21")
    @ResponseBody
    public void save21(@RequestHeader(value = "User-Agent", required = false)String user_agent){
        System.out.println(user_agent);
    }

    @RequestMapping("/quick22")
    @ResponseBody
    public void save22(@CookieValue(value = "JSESSIONID", required = false)String jsessionId){
        //cookie是header的一部分
        System.out.println(jsessionId);
    }

    @RequestMapping("/quick23")
    @ResponseBody
    public void save23(String username, MultipartFile uploadFile) throws IOException {
        //单文件上传，pom需要加，upload.jsp, spring-mvc.xml需要改，
        //userame对应表达中第一个输入框的名字
        //uploadFile对应文件输入框的名字
        System.out.println(username);
        System.out.println(uploadFile);

        //获取文件的名称
        String originalFileName = uploadFile.getOriginalFilename();
        //保存文件
        uploadFile.transferTo(new File("D:\\cyh\\work_java\\SSMTest\\003MySpringMvc\\账号.txt"));
    }

    @RequestMapping("/quick24")
    @ResponseBody
    public void save24(String username, MultipartFile uploadFile, MultipartFile uploadFile2) {
        //多文件上传，用不同的名字
        System.out.println(username);
        System.out.println(uploadFile);
        System.out.println(uploadFile2);
    }

    @RequestMapping("/quick25")
    @ResponseBody
    public void save25(String username, MultipartFile[] uploadFile) {
        //多文件上传，用相同的名字
        System.out.println(username);
        System.out.println(uploadFile);
    }
}
